package t.maxdabest;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FileIoActivity extends Activity implements OnClickListener {
	EditText editText1;
	Button buttonCreate;
	EditText editText2;
	Button buttonDisplay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button buttonCreate = (Button) findViewById(R.id.buttonCreate);
		buttonCreate.setText("Create File");
		buttonCreate.setOnClickListener(this);

		Button buttonDisplay = (Button)findViewById(R.id.buttonDisplay);
		buttonDisplay.setText("Display File");
		buttonDisplay.setOnClickListener(this);


	}
	public void onClick(View v) {
		// TODO Auto-generated method stub

		EditText editText1 = (EditText) findViewById(R.id.editText1);
		EditText editText2 = (EditText) findViewById(R.id.editText2);

		if (v.equals(buttonCreate)){
			// creates a file
			try {
				OutputStreamWriter out = new OutputStreamWriter(openFileOutput("File Name",Context.MODE_WORLD_READABLE));
				out.write(editText1.getText().toString());
				out.close();
				Toast.makeText(this,"Dis a toast to my fallen homies", Toast.LENGTH_SHORT).show();
			} catch (Exception e) {
				// TODO Auto-generated catch block

				e.printStackTrace(); 
			}
		}

		else if(v.equals(buttonDisplay))
		{
			
			InputStream in;
			try {
				in = openFileInput(editText2.getText().toString());
				BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
				String str;
				if(in != null){			
					while ((str = reader.readLine()) != null) 					{
						Toast.makeText(this,str,Toast.LENGTH_SHORT).show();

					}
					in.close();
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} //opens file



		}
	}
}
